-- UIGDInput2
-- Created by cheny3 Mar/07/2016
-- GD界面输入界面2（滚动容器多个输入框/两个按钮）

UIGDInput2 = class(" UIGDInput2", function()
    return cc.Layer:create();
end);

function UIGDInput2.create(gdType)
    return UIGDInput2.new(gdType);
end

-- 添加属性列表
local addAttribs = {"hp", "mp", "attack", "magic", "money", "gem",
    "search_point", "max_hp", "max_mp", "attrib_name", "attrib_value"};
-- 设置属性列表
local setAttribs = {"hp", "mp", "attack", "magic", "search_point",
    "max_hp", "max_mp", "gem", "money",};

-- 构造函数
function UIGDInput2:ctor(gdType)
    self:setName("UIGDInput2");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/GD/GDInput2.csb");
    self.node = node;
    self:addChild(node);

    self.gdType = gdType;

    -- 设置层级
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 100001);

    -- 滚动容器
    local scrollView = findChildByName(self.node, "CT/scroll_view");
    self.scrollView = scrollView;
    self.initHeight = scrollView:getInnerContainerSize().height;

    self.attribEditExList = {};

    -- 绘制
    self:redraw(node);

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize(node);
end

-- 获取所有输入项目
function UIGDInput2:getItems()
    if self.gdType == "add_attrib" then
        return addAttribs;
    elseif self.gdType == "set_attrib" then
        return setAttribs;
    else
        cclog("error debug type input!");
    end
end

-- 绘制
function UIGDInput2:redraw()
    local scrollView = self.scrollView;
    -- 清空所有节点
    scrollView:removeAllChildren();

    -- 获取项目
    local items = self:getItems();
    local totalNum = #items;

    --左边距
    local leftMargin = 200;

    -- 行间距
    local gap = 0;
    local widgetHeight = 60;

    local gd_loc = "";
    if self.gdType == "add_attrib" then
        gd_loc = "gd_add_";
    elseif self.gdType == "set_attrib" then
        gd_loc = "gd_set_";
    end

    -- 计算总高度
    local totalHeight = math.max(totalNum * (widgetHeight + gap), self.initHeight);
    scrollView:setInnerContainerSize(cc.size(scrollView:getInnerContainerSize().width, totalHeight));
    self.attribEditExList = {};
    for i = 1, #items do
        local child = cc.CSLoader:createNode("layout/setting/FieldNode.csb");
        child = replaceNodeWithWidget(child);
        child:setAnchorPoint(cc.p(0, 0));
        child:setPositionX(leftMargin);
        child:setPositionY(totalHeight - i * (widgetHeight + gap) + 20);
        local attribName = items[i];
        child:setName("gd_" .. attribName);

        -- 属性名
        local attribLabel = findChildByName(child, "fieldname");
        TextStyleM.setTextStyle(attribLabel, TextStyleM.TEXT_SIZE_SMALL , TextStyleM.TEXT_COLOR_BRIGHT_YELLOW, true);
        attribLabel:setString(getLocStr("gd_" .. attribName));

        -- 属性值输入框
        local attribEdit = findChildByName(child, "field_input");
        local attribEditBoxSize = attribEdit:getContentSize();
        local attribEditEx = ccui.EditBox:create(attribEditBoxSize, "images/ui/login/1.png");
        attribEditEx:setAnchorPoint(cc.p(0, 0));
        attribEditEx:setPosition(attribEdit:getPosition());
        attribEditEx:setFontName(getLocFontName());
        attribEditEx:setFontSize(TextStyleM.TEXT_SIZE_TINY);
        attribEditEx:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_NORMAL);
        attribEditEx:setPlaceHolder(getLocStr(gd_loc .. attribName));
        attribEditEx:setMaxLength(16);
        attribEditEx:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);

        attribEdit:getParent():addChild(attribEditEx);

        local ret = {["attrib_name"] = attribName, ["attrib_value"] = attribEditEx};

        table.insert(self.attribEditExList, ret);

        attribEdit:setVisible(false);
        self.scrollView:addChild(child);
    end
    self.scrollView:scrollToTop(0.1, true);
end

-- 属性增加
function UIGDInput2:addFields()
    -- 有两个属性是特殊的属性（输入的是属性名和属性值一对的， 特殊处理）
    local inputAttribName = nil;
    local inputAttribValue = nil;
    for _, value in pairs(self.attribEditExList) do
        local attribName = tostring(value.attrib_name);
        if attribName == "attrib_name" then
            inputAttribName = tostring(value.attrib_value:getText());
        elseif attribName == "attrib_value" then
            inputAttribValue = tonumber(value.attrib_value:getText());
        else
            local addVal = tonumber(value.attrib_value:getText());
            if addVal > 0 then
                GDM.add_attrib(attribName, addVal);
            end
        end
    end

    -- 输入属性名
    if inputAttribName ~= "" then
        inputAttribValue = iif(inputAttribValue == 0, 1, inputAttribValue);
        GDM.add_attrib(inputAttribName, inputAttribValue);
    end
end

-- 设置属性
function UIGDInput2:setFields()
    local inputAttribName = nil;
    local inputAttribValue = nil;
    for _, value in pairs(self.attribEditExList) do
        local attribName = tostring(value.attrib_name);
        if attribName == "attrib_name" then
            inputAttribName = tostring(value.attrib_value:getText());
        elseif attribName == "attrib_value" then
            inputAttribValue = tonumber(value.attrib_value:getText());
        else
            local addVal = tonumber(value.attrib_value:getText());
            if addVal > 0 then
                GDM.set_attrib(attribName, addVal);
            end
        end
    end
end

-- 点击确认事件
function UIGDInput2:okClickEvent()
    if self.gdType == "add_attrib" then
        self:addFields();
    elseif self.gdType == "set_attrib" then
        self:setFields();
    else
        cclog("error debug type input!");
    end
end

-- 注册点击事件
function UIGDInput2:registerTouchEvent()
    -- 确定按钮
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:okClickEvent();
            UIMgr.getCurrentScene():removeFormByName("UIGDInput2");
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("confirm"));

    -- 取消
    local btnCancel = findChildByName(self.node, "CT/btn_cancel");
    local function onCancelClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIGDInput2");
        end
    end
    btnCancel:addTouchEventListener(onCancelClick);
    TextStyleM.setTextStyle(btnCancel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnCancel:setTitleText(getLocStr("cancel"));
end

-- 适配
function UIGDInput2:resize(node)
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end